package com.psy.price.recorder.service;


import java.sql.Date;
import java.util.List;
import com.psy.price.recorder.domain.Record;
import com.psy.price.recorder.manager.RecordManager;
import com.psy.price.recorder.util.ISBNFileReader;
import com.psy.price.recorder.util.LogUtil;
import com.psy.price.recorder.util.PropertiesConfigReader;
import com.psy.price.recorder.util.TextBookSearchConfig;
import yenwen.bigheadfactory.validator.BetterIsbnValidator;
import yenwen.bigheadfactory.webservice.AmzAPI;
import yenwen.bigheadfactory.webservice.Constant;
import yenwen.bigheadfactory.webservice.DirectTextbookAPI;
import yenwen.bigheadfactory.webservice.OriginalHalfAPI;
import yenwen.bigheadfactory.webservice.dto.AmzOfferDTO;
import yenwen.bigheadfactory.webservice.dto.DirectTextbookInfoDTO;
import yenwen.bigheadfactory.webservice.dto.HalfOfferDTO;

public class SearchEngine {

	// public Record[] records = new Record[10];

	TextBookSearchConfig config = PropertiesConfigReader.getInstance()
			.getTextBookSearchConfig();

	public Record[] addRecords(String[] isbns) {

		Record[] records = new Record[isbns.length];
		String accessKey = "AKIAJU3MJ6ZCYL2RI3EQ"; // fan
		String secretKey = "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"; // fan
		AmzAPI amzAPI;
		DirectTextbookAPI dtAPI;
		OriginalHalfAPI halfAPI;

		dtAPI = new DirectTextbookAPI(config.getProxyHost(), config
				.getProxyPort());
		halfAPI = new OriginalHalfAPI(config.getAppID(), config.getProxyHost(),
				config.getProxyPort());

		List<AmzOfferDTO[]> amzOfferList = null;
		List<DirectTextbookInfoDTO> buybackOfferList = null;
		BetterIsbnValidator validator = new BetterIsbnValidator();

		try {
			amzAPI = new AmzAPI(accessKey, secretKey, Constant.DirectProxyHost,
					Constant.DefaultProxyPort, false);
			amzOfferList = amzAPI.getAmzBookOffersSoap(isbns);
			for (int i = 0; i < isbns.length; i++) {

				HalfOfferDTO[] halfOffer = new HalfOfferDTO[5];
				AmzOfferDTO[] amzOffer = new AmzOfferDTO[5];
				java.util.Date now = new java.util.Date();

				if (null != amzOfferList) {

					// halfOffer = halfAPI.getHalfProducts(isbns[i]);
					amzOffer = amzOfferList.get(i);
					int salesRank = Integer.parseInt(amzOfferList.get(i)[0]
							.getSalesRank());

					records[i] = new Record(
							validator.convertToISBN13(isbns[i]), salesRank,
							dtAPI.getBuyBack(isbns[i]), amzOffer, halfOffer,
							new Date(now.getTime()));

				}
			}

		} catch (Exception e) {
			LogUtil.getLogger().error(e.getMessage());
			e.printStackTrace();
		}
		return records;

		/*
		 * int i = 0; for (AmzOfferDTO[] book : amzOfferList) { this.records[i]
		 * = new Record(); if (book != null) { this.records[i].setA(book); }
		 * i++; } i=0; for (String isbn :isbns){
		 * records[i].setBB(dtAPI.getBuyBack(isbn)); i++; }
		 */

	}

	public String[] ISBNValidate(String[] isbns) {

		BetterIsbnValidator validator = new BetterIsbnValidator();

		for (int i = 0; i < isbns.length; i++) {
			if (validator.isValidISBN13(isbns[i]))
				isbns[i] = validator.convertToISBN10(isbns[i]);
			else if (validator.isValidISBN10(isbns[i]) == false) {
				LogUtil.getLogger().error("isbn: " + isbns[i] + " is invalid");

			}

		}

		return isbns;

	}

	public static void main(String[] args) {
		List<String> list = ISBNFileReader.getInstance().getList();
		SearchEngine s = new SearchEngine();
		//String [] ISBNs = {"0471172758","0470387742","0393934233", "1429224266","0471593486","0470014954","0470844922","0470292288","047074250X"};

		BetterIsbnValidator validator = new BetterIsbnValidator();

		//System.out.println(s.records[0]);
		String[] ISBNs= new String[10];
		String[] ISBNs2 = new String[list.size()%10];
		for (int i = 0; i < list.size(); i++) {
			ISBNs[i%10] = validator.convertToISBN10(list.get(i));
			if (i%10 == 9){

				RecordManager.getinstance().addRecords(s.addRecords(ISBNs));
				System.out.println( list.get(i)+": done");
			}

			if (i>(list.size()-list.size()%10-1)){
				ISBNs2[i%10] = validator.convertToISBN10(list.get(i));
				//System.out.println( ISBNs2[i%10]);
			}

			if (i == (list.size() - 1)) {
				RecordManager.getinstance().addRecords(s.addRecords(ISBNs2));
				System.out.println(list.get(i)+": Last one done");
			}

			if (i!=0 && i % 100 == 0) {
				try {
					System.out.println("waiting");
					Thread.sleep(300000);
				} catch (InterruptedException e) {

					LogUtil.getLogger().error(e);
				}

			}
		}



	}


}
